package com.sicnu.cheer.leaugebar.leaguemodle.activity;

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.sicnu.cheer.generalmodule.bean.AlbumBean;
import com.sicnu.cheer.generalmodule.bean.LeagueBean;
import com.sicnu.cheer.generalmodule.bean.MyUser;
import com.sicnu.cheer.generalmodule.config.Constant;
import com.sicnu.cheer.generalmodule.util.NetWorkUtil;
import com.sicnu.cheer.generalmodule.util.UIHelper;
import com.sicnu.cheer.leaugebar.leaguemodle.R;
import com.sicnu.cheer.leaugebar.leaguemodle.fragment.HisFragment;
import com.sicnu.cheer.leaugebar.leaguemodle.fragment.ImgFragment;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

public class LeagueMainActivity extends TabCommonActivity {

    public ArrayList<String> urls;
    private ImgFragment imgFr;
    private ProgressDialog progressDialog;
    private int albumState=0;//0无相册无照片，1有相册无照片，2有相册和照片

    @Override
    public int getContentViewId() {
        return R.layout.activity_league_main;
    }

    @Override
    public String getTitleString() {
        return "社团风采";
    }

    @Override
    protected Map<String, Fragment> getFragments() {
        final Map<String,Fragment> map=new HashMap<>();
        imgFr = new ImgFragment();
        map.put("照片集", imgFr);
        map.put("历史活动",new HisFragment());
        return map;
    }

    @Override
    protected void initView() {
        super.initView();
        MyUser myUser=MyUser.getCurrentUser(mThis,MyUser.class);
        final ImageView leagueLogoIV = (ImageView) findViewById(R.id.league_logo_iv);
        if (myUser!=null){
            final LeagueBean league = myUser.getLeague();
            if (league!=null){
                BmobQuery<LeagueBean> query=new BmobQuery();
                query.addWhereEqualTo("objectId",league.getObjectId());
                query.findObjects(mThis, new FindListener<LeagueBean>() {
                    @Override
                    public void onSuccess(List<LeagueBean> list) {
                        if (list!=null&&list.size()>0) {
                            Glide.with(mThis).load(list.get(0).getLogo()).placeholder(R.mipmap.default_logo).into(leagueLogoIV);
                        }
                    }

                    @Override
                    public void onError(int i, String s) {

                    }
                });
            }
        }
    }

    @Override
    protected void initData() {
        super.initData();
        urls = new ArrayList<>();

        progressDialog = new ProgressDialog(mThis);
        progressDialog.setMessage("加载中...");
    }

    @Override
    protected void afterViews() {
        super.afterViews();
        //获取相册中的7张照片
        LeagueBean league = MyUser.getCurrentUser(mThis, MyUser.class).getLeague();
        if (league!=null){
            if (!NetWorkUtil.isConnect(mThis)){
                UIHelper.ToastMessage(mThis,"没有网络，请设置您的网络");
                imgFr.refreshLayout(-1);
                return;
            }
            progressDialog.show();
            BmobQuery<AlbumBean> query=new BmobQuery();
            query.addWhereEqualTo("owner",league);
            query.findObjects(mThis, new FindListener<AlbumBean>() {
                @Override
                public void onSuccess(List<AlbumBean> list) {
                    progressDialog.dismiss();
                    imgFr.refreshLayout(albumState);
                    if (list!=null&&list.size()>0) {
                        albumState=1;
                        for (AlbumBean bean:list){
                            if (bean.getImgUrls()!=null&&bean.getImgUrls().size()>0) {
                                albumState=2;
                                urls.addAll(bean.getImgUrls());
                                if (urls.size() > 7) {
                                    break;
                                }
                            }
                        }
                    }
                    imgFr.urls=urls;
                    imgFr.refreshLayout(albumState);
                }

                @Override
                public void onError(int i, String s) {
                    progressDialog.dismiss();
                    imgFr.refreshLayout(-1);
                }
            });
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode== Constant.ALBUM_RESULT_FROM){
            afterViews();
        }
    }
}
